import request from '@/utils/request'
import type { 
  User, 
  Role, 
  Menu, 
  Tenant, 
  TenantConfig,
  Permission,
  UserListParams,
  UserListResponse,
  RoleListParams,
  RoleListResponse,
  MenuListParams,
  TenantListParams,
  TenantListResponse
} from '@/types/api'

// 用户管理相关接口
export const getUserPage = (params: UserListParams): Promise<UserListResponse> => {
  return request.get('/users/page', { params })
}

export const getUserList = (): Promise<User[]> => {
  return request.get('/users')
}

export const getUserById = (id: number): Promise<User> => {
  return request.get(`/users/${id}`)
}

export const createUser = (data: any): Promise<User> => {
  return request.post('/users', data)
}

export const updateUser = (id: number, data: any): Promise<User> => {
  return request.put(`/users/${id}`, data)
}

export const deleteUser = (id: number): Promise<void> => {
  return request.delete(`/users/${id}`)
}

export const updateUserStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/users/${id}/status`, { status })
}

export const assignUserRoles = (userId: number, roleIds: number[]): Promise<void> => {
  return request.put(`/users/${userId}/roles`, { roleIds })
}

export const getUserRoles = (userId: number): Promise<Role[]> => {
  return request.get(`/users/${userId}/roles`)
}

// 角色管理相关接口
export const getRolePage = (params: RoleListParams): Promise<RoleListResponse> => {
  return request.get('/roles/page', { params })
}

export const getRoleList = (): Promise<Role[]> => {
  return request.get('/roles')
}

export const getRoleById = (id: number): Promise<Role> => {
  return request.get(`/roles/${id}`)
}

export const createRole = (data: any): Promise<Role> => {
  return request.post('/roles', data)
}

export const updateRole = (id: number, data: any): Promise<Role> => {
  return request.put(`/roles/${id}`, data)
}

export const deleteRole = (id: number): Promise<void> => {
  return request.delete(`/roles/${id}`)
}

export const updateRoleStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/roles/${id}/status`, { status })
}

export const assignRolePermissions = (roleId: number, permissionIds: number[]): Promise<void> => {
  return request.put(`/roles/${roleId}/permissions`, { permissionIds })
}

export const getRolePermissions = (roleId: number): Promise<Permission[]> => {
  return request.get(`/roles/${roleId}/permissions`)
}

// 菜单管理相关接口
export const getMenuPage = (params: MenuListParams): Promise<any> => {
  return request.get('/menus/page', { params })
}

export const getMenuList = (): Promise<Menu[]> => {
  return request.get('/menus')
}

export const getMenuById = (id: number): Promise<Menu> => {
  return request.get(`/menus/${id}`)
}

export const createMenu = (data: any): Promise<Menu> => {
  return request.post('/menus', data)
}

export const updateMenu = (id: number, data: any): Promise<Menu> => {
  return request.put(`/menus/${id}`, data)
}

export const deleteMenu = (id: number): Promise<void> => {
  return request.delete(`/menus/${id}`)
}

export const updateMenuStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/menus/${id}/status`, { status })
}

export const getMenuTree = (): Promise<Menu[]> => {
  return request.get('/menus/tree')
}

// 租户管理相关接口
export const getTenantPage = (params: TenantListParams): Promise<TenantListResponse> => {
  return request.get('/tenants/page', { params })
}

export const getTenantList = (): Promise<Tenant[]> => {
  return request.get('/tenants')
}

export const getTenantById = (id: number): Promise<Tenant> => {
  return request.get(`/tenants/${id}`)
}

export const createTenant = (data: any): Promise<Tenant> => {
  return request.post('/tenants', data)
}

export const updateTenant = (id: number, data: any): Promise<Tenant> => {
  return request.put(`/tenants/${id}`, data)
}

export const deleteTenant = (id: number): Promise<void> => {
  return request.delete(`/tenants/${id}`)
}

export const updateTenantStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/tenants/${id}/status`, { status })
}

export const updateTenantConfig = (id: number, config: TenantConfig): Promise<void> => {
  return request.put(`/tenants/${id}/config`, config)
}

export const getTenantConfig = (id: number): Promise<TenantConfig> => {
  return request.get(`/tenants/${id}/config`)
}

// 系统配置相关接口
export const getSystemConfig = (): Promise<any> => {
  return request.get('/system/config')
}

export const updateSystemConfig = (data: any): Promise<void> => {
  return request.put('/system/config', data)
}

// 系统日志相关接口
export const getSystemLogs = (params: any): Promise<any> => {
  return request.get('/system/logs', { params })
}

export const getSystemLogById = (id: number): Promise<any> => {
  return request.get(`/system/logs/${id}`)
}

export const clearSystemLogs = (): Promise<void> => {
  return request.delete('/system/logs')
}

// 数据备份相关接口
export const getBackupList = (): Promise<any[]> => {
  return request.get('/system/backups')
}

export const createBackup = (): Promise<void> => {
  return request.post('/system/backups')
}

export const restoreBackup = (backupId: string): Promise<void> => {
  return request.post(`/system/backups/${backupId}/restore`)
}

export const deleteBackup = (backupId: string): Promise<void> => {
  return request.delete(`/system/backups/${backupId}`)
}

// 系统监控相关接口
export const getSystemInfo = (): Promise<any> => {
  return request.get('/system/info')
}

export const getSystemMetrics = (): Promise<any> => {
  return request.get('/system/metrics')
}

export const getSystemHealth = (): Promise<any> => {
  return request.get('/system/health')
}